/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package libCore;

import com.jezhumble.javasysmon.CpuTimes;
import com.jezhumble.javasysmon.JavaSysMon;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Salm
 */
public class SysMonitor {
    private SysMonitor() {}
    
    private static final JavaSysMon monitor = new JavaSysMon();
    private static CpuTimes lastCPU;
    private static float cpuUsg = 0.f;
    
    static
    {
        lastCPU = monitor.cpuTimes();
    }
    
    public static float getCPUUsage()
    {
        return cpuUsg;
    }
    
    public static void start()
    {
        Executors.newSingleThreadScheduledExecutor()
                .scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                synchronized (monitor)
                {
                    CpuTimes cpu = monitor.cpuTimes();
                    cpuUsg = cpu.getCpuUsage(lastCPU);
                    lastCPU = cpu;
                }
            }
        }, 1, 1, TimeUnit.SECONDS);
    }
}
